/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-03-10 10:27:53
 * @Descripttion: 审批出入库管理
 */

import { Component, OnInit } from '@angular/core';
import { ExamineApproveWareService } from './examine-approve-ware.service'
import { Router, ActivatedRoute } from '@angular/router';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-examine-approve-ware',
  templateUrl: './examine-approve-ware.component.html',
  styleUrls: ['./examine-approve-ware.component.scss']
})
export class ExamineApproveWareComponent implements OnInit {

  constructor(
    private ExamineApproveWareService: ExamineApproveWareService,
    private NzModalService: NzModalService,
    private Router: Router
  ) { }

  ngOnInit() {
    this.GetSearchPageList();
    this.GetEnumWareInState();
  }
  // search start
  ApplyCode: string = "";
  ApplyUser: string = "";
  ExState: any = "";
  searchType: any = "";
  DocumentType: any = "";
  statusList: string = "1,2,3,4,5";
  sort = "reqDate,desc";
  delList = []
  // 审批状态
  wareInStateList = [];
  GetEnumWareInState() {
    let data = {
      enumName: "MaterialInoutStatusEnum"
    }
    this.ExamineApproveWareService.getEnum(data).subscribe(res => {
      for (const key in res) {
        var e = { key: key, value: res[key] }
        this.wareInStateList.push(e);
      }
      console.log(this.wareInStateList);
    });
  }
  // 单据类型
  MaterialInBillTypeEnum = [];
  GetMaterialInBillTypeEnum(type) {
    let MaterialInBillTypeEnumPri = [];
    let data = {};
    if (type == "") {
      this.MaterialInBillTypeEnum = [];
      return;
    }
    if (type == 1) {
      data = {
        enumName: "MaterialInBillTypeEnum"
      }
    }
    else if (type == 2) {
      data = {
        enumName: "MaterialOutBillTypeEnum"
      }
    }
    this.ExamineApproveWareService.getEnum(data).subscribe(res => {
      for (const key in res) {
        var e = { key: key, value: res[key] }
        MaterialInBillTypeEnumPri.push(e);
      }
      this.MaterialInBillTypeEnum = MaterialInBillTypeEnumPri
      console.log(this.MaterialInBillTypeEnum);
    });
  }

  selectedStatusList(data) {
    this.GetSearchPageList();
  }

  selectedDocumentCategories(data) {
    this.GetSearchPageList();
  }

  selectedDocumentType(data) {
    this.GetSearchPageList();
  }
  selectedType(data) {
    this.DocumentType = "";

    this.GetMaterialInBillTypeEnum(data);

    this.GetSearchPageList();
  }
  // search end

  // table start

  isAllChecked: boolean = false;
  approvalDisabled: boolean = true;
  approvalInfoDisabled: boolean = true;
  checkAll(value: boolean): void {
    this.listData.forEach(item => {
      item.checked = value;
    });
  }
  selId: string = "";
  detailType: number = 0
  SelectRow(): void {
    let _isAllCheck = true;
    this.listData.forEach(item => {
      if (!item.checked) {
        _isAllCheck = false;
      }
    })
    this.isAllChecked = _isAllCheck;
    var checkCount = this.listData.filter(item => item.checked).length;

    if (checkCount === 1) {
      this.delList = this.listData.filter(s => s.checked);
      if (this.delList[0].status == 1 || this.delList[0].status == 5) {
        this.approvalDisabled = false;
      }
      else {
        this.approvalDisabled = true;
      }
      this.approvalInfoDisabled = false;
      if (this.delList.length != 0) {
        this.selId = this.delList[0].id;
        this.detailType = this.delList[0].type;
      }
    }
    else {
      this.approvalDisabled = true;
      this.approvalInfoDisabled = true;
    }
  }

  listData = [];
  pageIndex: any = 1;//提交的页码
  pagesize: any = 10;//table每页条数
  totalSize: any = '';//总共条数
  clickPage(pageIndex) {
    this.pageIndex = pageIndex;
    this.GetSearchPageList();
  }
  clickSize(pageSize) {
    this.pagesize = pageSize
    this.GetSearchPageList();
  }
  GetSearchPageList() {
    let data = {
      billType: this.DocumentType,
      code: this.ApplyCode,
      inoutStatus: "",
      statusList: this.statusList,
      nickName: this.ApplyUser,
      status: this.ExState,
      type: this.searchType,
      page: this.pageIndex - 1,
      size: this.pagesize,
      sort: this.sort
    }
    this.ExamineApproveWareService.apiList(data).subscribe(res => {
      this.approvalDisabled = true;
      this.approvalInfoDisabled = true;
      res.content.forEach(s => s.checked == false);
      this.listData = res.content
      this.totalSize = res.totalElements
    });
  }
  // table end

  // jump page
  approval() {
    if (this.detailType == 1) {
      this.Router.navigate(['/material/warehouse/approvalin', this.selId])
    } else if (this.detailType == 2) {
      this.Router.navigate(['/material/warehouse/approvalout', this.selId])
    }
  }
  detail() {
    if (this.detailType == 1) {
      this.Router.navigate(['/material/warehouse/applywarewarehousingdetail', this.selId]);
    }
    else if (this.detailType == 2) {
      this.Router.navigate(['/material/warehouse/applywareoutofstockdetail', this.selId]);
    }
  }
}
